﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Billing.Win
{
    [ToolboxItem(true)]
    public partial class ucClientPhoneActions : Billing.WindowsBase.CustomControls.BaseControl
    {
        private const string msgOperSuccess = "ოპერაცია წარმატებით დასრულდა";

        Billing.DataObjects.BillingDataContext context = new Billing.DataObjects.BillingDataContext();

        private ClientPhoneActions clientPhoneActions;
        public ClientPhoneActions ClientPhoneActions
        {
            get
            {
                return clientPhoneActions;
            }
            set
            {
                clientPhoneActions = value;
                switch (value)
                {
                    case ClientPhoneActions.Remove:
                        grbxNewOwner.Visible = false;
                        break;
                    case ClientPhoneActions.Move:
                        grbxNewOwner.Visible = true;
                        break;
                }
            }
        }

        private Billing.DataObjects.ClientsView ownerClient;
        private Billing.DataObjects.ClientsView newOwnerClient;
        private Billing.DataObjects.AccountsView account;
        public Billing.DataObjects.ClientsView OwnerClient
        {
            get
            {
                return this.ownerClient;
            }
            set
            {
                this.ownerClient = value;
            }
        }
        public Billing.DataObjects.ClientsView NewOwnerClient
        {
            get
            {
                return this.newOwnerClient;
            }
            set
            {
                this.newOwnerClient = value;
            }
        }
        public Billing.DataObjects.AccountsView Account
        {
            get
            {
                return this.account;
            }
            set
            {
                this.account = value;
            }
        }


        bool isSwitchOnDateEdited = false;
        bool setSwitchOnDateEdited = false;
        bool initialized = false;

        public DateTime RemoveDate
        {
            get
            {
                return valRemoveDate.DateTime;
            }
            set
            {
                valRemoveDate.DateTime = value;
                setSwitchOnDateEdited = false;
                if (!isSwitchOnDateEdited)
                {
                    SwitchOnDate = value;
                }
                setSwitchOnDateEdited = true;
            }
        }
        public DateTime SwitchOnDate
        {
            get
            {
                return valSwitchOnDate.DateTime;
            }
            set
            {
                valSwitchOnDate.DateTime = value;

                if (setSwitchOnDateEdited)
                    isSwitchOnDateEdited = true && initialized;
            }
        }

        public ucClientPhoneActions()
        {
            InitializeComponent();
            CaptionVisibled = false;
        }

        public void Initialize(ClientPhoneActions clientPhoneActions)
        {
            Initialize(clientPhoneActions, null);
        }

        public void Initialize(ClientPhoneActions clientPhoneActions,
            Billing.DataObjects.ClientsView ownerClient)
        {
            Initialize(clientPhoneActions, ownerClient, null);
        }

        public void Initialize(ClientPhoneActions clientPhoneActions,
            Billing.DataObjects.ClientsView ownerClient,
            Billing.DataObjects.AccountsView accountsView)
        {
            Initialize(clientPhoneActions, ownerClient, accountsView, null);
        }

        public void Initialize(ClientPhoneActions clientPhoneActions,
            Billing.DataObjects.ClientsView ownerClient,
            Billing.DataObjects.AccountsView accountsView,
            DateTime? removeDate)
        {
            Initialize(clientPhoneActions, ownerClient, accountsView, removeDate, null);
        }

        public void Initialize(ClientPhoneActions clientPhoneActions,
            Billing.DataObjects.ClientsView ownerClient,
            Billing.DataObjects.AccountsView accountsView,
            DateTime? removeDate,
            Billing.DataObjects.ClientsView newOwnerClient)
        {
            Initialize(clientPhoneActions, ownerClient, accountsView, removeDate, newOwnerClient, null);
        }

        public void Initialize(ClientPhoneActions clientPhoneActions,
            Billing.DataObjects.ClientsView ownerClient,
            Billing.DataObjects.AccountsView accountsView,
            DateTime? removeDate,
            Billing.DataObjects.ClientsView newOwnerClient,
            DateTime? switchOnDate)
        {
            isSwitchOnDateEdited = false;
            setSwitchOnDateEdited = false;
            this.ClientPhoneActions = clientPhoneActions;
            this.ownerClient = ownerClient;
            this.newOwnerClient = newOwnerClient;
            this.account = accountsView;

            //-------- Owner Client Controls
            valOwnerClientIdentificationNumber.Enabled = ownerClient == null;
            valOwnerClientName.Enabled = valOwnerClientIdentificationNumber.Enabled;
            btSelectOwnerClient.Visible = valOwnerClientIdentificationNumber.Enabled;
            SetOwnerClientValues(ownerClient);

            //-------- Owner Client Phone Controls
            valAccountNumber.Enabled = accountsView == null;
            btSelectOwnerClientPhone.Enabled = valAccountNumber.Enabled;
            SetAccountValues(accountsView);

            //-------- Owner Client Phone Remove Date Controls
            valRemoveDate.Enabled = removeDate == null;
            SetRemoveDateValues(removeDate);

            //-------- New Owner Client Controls
            valNewOwnerClientIdentificationNumber.Enabled = newOwnerClient == null;
            valNewOwnerClientName.Enabled = valNewOwnerClientIdentificationNumber.Enabled;
            btSelectNewOwnerClient.Enabled = valNewOwnerClientIdentificationNumber.Enabled;
            SetNewOwnerClientValues(newOwnerClient);

            //-------- New Owner Client Phone Start Date Controls
            valSwitchOnDate.Enabled = switchOnDate == null;
            SetSwitchOnDateValues(switchOnDate == null ? RemoveDate.AddDays(1) : switchOnDate);

            if (clientPhoneActions == ClientPhoneActions.Remove)
            {
                grbxNewOwner.Visible = false;
            }
            setSwitchOnDateEdited = true;
            initialized = true;
        }

        private void SetOwnerClientValues(Billing.DataObjects.ClientsView ownerClient)
        {
            if (ownerClient != null)
            {
                valOwnerClientIdentificationNumber.Value = ownerClient.IdentificationNumber;
                valOwnerClientName.Value = ownerClient.Name;
            }
            else
            {
                valOwnerClientIdentificationNumber.Value = null;
                valOwnerClientName.Value = null;
            }
        }

        private void SetAccountValues(Billing.DataObjects.AccountsView accountsView)
        {
            if (accountsView != null)
            {
                valAccountNumber.Value = accountsView.AccountNumber;
            }
            else
            {
                valAccountNumber.Value = null;
            }
        }

        private void SetRemoveDateValues(DateTime? removeDate)
        {
            if (removeDate != null)
            {
                RemoveDate = removeDate.Value;
            }
        }

        private void SetNewOwnerClientValues(Billing.DataObjects.ClientsView newOwnerClient)
        {
            if (newOwnerClient != null)
            {
                valNewOwnerClientIdentificationNumber.Value = newOwnerClient.IdentificationNumber;
                valNewOwnerClientName.Value = newOwnerClient.Name;
            }
            else
            {
                valNewOwnerClientIdentificationNumber.Value = null;
                valNewOwnerClientName.Value = null;
            }
        }

        private void SetSwitchOnDateValues(DateTime? switchOnDate)
        {
            if (switchOnDate != null)
            {
                SwitchOnDate = switchOnDate.Value;
            }
        }

        private DateTime GetAvailabeMinRemoveDate(
            ClientPhoneActions clientPhoneActions,
            Billing.DataObjects.AccountsView account)
        {
            if (DesignMode) return DateTime.Today;

            DateTime? resultDate = null;
            context.AccountsGetMinAvailableRemoveDate(account.ClientID, account.AccountNumberID, ref resultDate);
            return resultDate.Value;
        }

        private void valSwitchOnDate_ValueChanged(object sender, EventArgs e)
        {
            if (setSwitchOnDateEdited)
                isSwitchOnDateEdited = true && initialized;
        }

        private void valRemoveDate_ValueChanged(object sender, EventArgs e)
        {
            setSwitchOnDateEdited = false;
            if (!isSwitchOnDateEdited)
            {
                SwitchOnDate = RemoveDate.AddDays(1);
            }
            else
            {
                if (SwitchOnDate < RemoveDate)
                {
                    SwitchOnDate = RemoveDate.AddDays(1);
                }
            }
            setSwitchOnDateEdited = true;
        }

        private void DoSelect(object sender, EventArgs e)
        {
            switch (Convert.ToString(((Control)(sender)).Tag))
            {
                case "SelectClient":
                    using (ClientsSelectionForm f = new ClientsSelectionForm())
                    {
                        f.PrepareForSelectDialogStyle();


                        if (f.ShowDialog(this) == DialogResult.OK)
                        {
                            if (f.SelectedObjects.Count > 0)
                            {
                                Billing.DataObjects.ClientsView client = ((Billing.DataObjects.ClientsView)f.SelectedObjects[0].CurrentObject);
                                OwnerClient = client;

                                SetOwnerClientValues(OwnerClient);
                            }
                        }
                    }
                    break;
                case "SelectPhone":
                    using (AccountsSelectionForm f = new AccountsSelectionForm())
                    {
                        f.PrepareForSelectDialogStyle();


                        if (f.ShowDialog(this) == DialogResult.OK)
                        {
                            if (f.SelectedObjects.Count > 0)
                            {
                                Billing.DataObjects.AccountsView accNumber = ((Billing.DataObjects.AccountsView)f.SelectedObjects[0].CurrentObject);
                                Account = accNumber;

                                SetAccountValues(Account);
                            }
                        }
                    }
                    break;
                case "SelectNewClient":
                    using (ClientsSelectionForm f = new ClientsSelectionForm())
                    {
                        f.PrepareForSelectDialogStyle();

                        if (f.ShowDialog(this) == DialogResult.OK)
                        {

                            if (f.SelectedObjects.Count > 0)
                            {
                                Billing.DataObjects.ClientsView client = ((Billing.DataObjects.ClientsView)f.SelectedObjects[0].CurrentObject);
                                NewOwnerClient = client;

                                SetNewOwnerClientValues(NewOwnerClient);
                            }
                        }
                    }
                    break;
            }
        }

        private void TBM_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            try
            {
                switch (e.Tool.Key)
                {
                    case "Save":    // ButtonTool
                        switch (ClientPhoneActions)
                        {
                            case ClientPhoneActions.Remove:
                                if (DesignMode) return;
                                context.AccountsRemove(Account.ID, RemoveDate);
                                base.ShowInformation(msgOperSuccess);
                                break;
                            case ClientPhoneActions.Move:
                                if (DesignMode) return;
                                context.AccountsMove(Account.ID, OwnerClient.ID, RemoveDate, NewOwnerClient.ID, SwitchOnDate, valAddLines.Checked);
                                base.ShowInformation(msgOperSuccess);
                                break;
                        }

                        break;

                    case "Cancel":    // ButtonTool
                        // Place code here
                        break;
                    case "Calculator":

                        if (newOwnerClient == null)
                        {
                            ShowWarning("დანიშნულების კლიენტი არ არის მითითებული");
                            return;
                        }

                        AnalysisSubject analysisSubject = new AnalysisSubject();
                        analysisSubject.AnalysisType = AnalysisType.AccountMove;
                        if (ClientPhoneActions == ClientPhoneActions.Remove)
                            analysisSubject.DestinationClient = OwnerClient;
                        else
                            analysisSubject.DestinationClient = newOwnerClient;
                        analysisSubject.SourceAccount = account;
                        analysisSubject.StartDate = RemoveDate;
                        analysisSubject.EndDate = DateTime.Today;

                        using (CalculatorForm f = new CalculatorForm())
                        {
                            f.Analyse(analysisSubject);
                        }
                        break;
                }
            }
            catch (Exception x)
            {
                base.ShowError(x);
            }
        }

        private void valOwnerClientIdentificationNumber_Validating(object sender, CancelEventArgs e)
        {
            if (DesignMode) return;

            int? clientID = null;

            context.ClientsGetClientByIdentificationNumber(Convert.ToString(valOwnerClientIdentificationNumber.Value), ref clientID);

            if (clientID == -1)
            {
                DoSelect(this, EventArgs.Empty);
                return;
            }
            else if (clientID != null)
            {
                OwnerClient = context.GetClientViewByID(clientID.Value);
            }
            else
            {
                OwnerClient = null;
            }

            SetOwnerClientValues(OwnerClient);
        }

        private void valNewOwnerClientIdentificationNumber_Validating(object sender, CancelEventArgs e)
        {
            if (DesignMode) return;

            int? clientID = null;

            context.ClientsGetClientByIdentificationNumber(Convert.ToString(valNewOwnerClientIdentificationNumber.Value), ref clientID);

            if (clientID != null)
            {
                NewOwnerClient = context.GetClientViewByID(clientID.Value);
            }
            else
            {
                NewOwnerClient = null;
            }

            SetNewOwnerClientValues(NewOwnerClient);
        }

        private void valOwnerClientIdentificationNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                valOwnerClientName.Select();
            }
        }

        private void valNewOwnerClientIdentificationNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                valNewOwnerClientName.Select();
            }
        }

        private void valAccountNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                valRemoveDate.Select();
            }
        }

        private void valAccountNumber_Validating(object sender, CancelEventArgs e)
        {
            if (DesignMode) return;

            if (OwnerClient == null) return;

            if (string.IsNullOrEmpty(Convert.ToString(valAccountNumber.Value))) return;

            Billing.DataObjects.AccountNumbersView accountNumber = context.GetAccountNumberByNumber(1, Convert.ToString(valAccountNumber.Value));
            if (accountNumber == null)
            {
                Account = null;
            }

            Account = context.GetAccountByClient(OwnerClient.ID, accountNumber.ID);

            SetAccountValues(Account);
        }

    }
}
